package com.mingtu.guards.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.mingtu.common.Eventbus.MyEventBus;
import com.mingtu.common.adapter.MyFragmentPagerAdapter;
import com.mingtu.common.entity.TabEntity;
import com.mingtu.common.utils.CalendarPickerUtils;
import com.mingtu.common.utils.UIUtils;
import com.mingtu.guards.R;
import com.mingtu.guards.base.MyBaseActivity;
import com.mingtu.guards.fragment.FireNoticeFragment;
import com.mingtu.guards.utils.MyConstant;
import com.mingtu.guards.utils.MySPTools;
import com.mingtu.guards.view.FilterCancleView;
import com.mingtu.guards.view.FilterSureView;
import com.mingtu.guards.view.TimeFilterView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

public class IndexFireNoticeActivity extends MyBaseActivity {

    @BindView(R.id.common_tab)
    CommonTabLayout commonTab;
    @BindView(R.id.iv_filte)
    ImageView ivFilte;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    private static final String[] mTitles = {"待处理", "处理中", "已处理"};
    @BindView(R.id.layout_right_drawer)
    LinearLayout layoutRightDrawer;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    @BindView(R.id.time_filter_time)
    TimeFilterView timeFilterTime;
    @BindView(R.id.filter_cancle_view)
    FilterCancleView filterCancleView;
    @BindView(R.id.filter_sure_view)
    FilterSureView filterSureView;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private String beginTime;
    private String endTime;
    private FireNoticeFragment fireNoticeFragment1;
    private FireNoticeFragment fireNoticeFragment2;
    private FireNoticeFragment fireNoticeFragment3;
    private boolean isMessageEnter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_index_fire_notice;
    }

    @Override
    protected void initView() {
        setModuleTitle("森林防火提醒");
    }

    @Override
    protected void initData() {
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }
        fireNoticeFragment1 = FireNoticeFragment.getInstance(0);
        fireNoticeFragment2 = FireNoticeFragment.getInstance(1);
        fireNoticeFragment3 = FireNoticeFragment.getInstance(2);

        mFragments.add(fireNoticeFragment1);
        mFragments.add(fireNoticeFragment2);
        mFragments.add(fireNoticeFragment3);

        viewPager.setOffscreenPageLimit(mFragments.size());
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragments));
        viewPager.setCurrentItem(0);
        viewPager.addOnPageChangeListener(onPageChangeListener);

        commonTab.setTextBold(1);
        commonTab.setTabData(mTabEntities);
        commonTab.setOnTabSelectListener(onTabSelectListener);

        EventBus.getDefault().register(this);

        timeFilterTime.setOnTimeResultListener(new TimeFilterView.OnTimeResultListener() {
            @Override
            public void onResult(Date startDate, Date endDate) {
                beginTime = UIUtils.getTime3(startDate);
                endTime = UIUtils.getTime3(endDate);

                SPStaticUtils.put(MySPTools.superJobFliteBeginTime, beginTime);
                SPStaticUtils.put(MySPTools.superJobFliteEndTime, endTime);
            }
        });

        filterCancleView.setOnClickListener(new FilterCancleView.OnClickListener() {
            @Override
            public void onClickCancel() {
                closeDrawers();
            }
        });
        filterSureView.setOnClickListener(new FilterSureView.OnClickListener() {
            @Override
            public void onClickReset() {
                closeDrawers();
                resetData();
                CalendarPickerUtils.getInstance().resetData();
            }

            @Override
            public void onClickSure() {
                boolean isResumed1 = fireNoticeFragment1.isResumed(); //判断fragment是否已经加载
                boolean isResumed2 = fireNoticeFragment2.isResumed();
                boolean isResumed3 = fireNoticeFragment3.isResumed();

                if (isResumed1) {
                    fireNoticeFragment1.filtJob();
                }
                if (isResumed2) {
                    fireNoticeFragment2.filtJob();
                }
                if (isResumed3) {
                    fireNoticeFragment3.filtJob();
                }
                closeDrawers();
            }
        });
    }

    @Override
    protected void getData() {
        isMessageEnter = getIntent().getBooleanExtra("isMessageEnter", false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onClickedTopLeftButtton(View view) {
        super.onClickedTopLeftButtton(view);
        if (isMessageEnter) {
            if (ActivityUtils.isActivityExistsInStack(MainActivity.class)) {
                ActivityUtils.startActivity(MainActivity.class, R.anim.right_in, R.anim.left_out);
                ActivityUtils.finishToActivity(MainActivity.class, false);
            } else {
                ActivityUtils.startActivity(MainActivity.class, R.anim.right_in, R.anim.left_out);
            }
        }
    }

    @Override
    protected void onKeyDownEvnet() {
        super.onKeyDownEvnet();
        if (isMessageEnter) {
            if (ActivityUtils.isActivityExistsInStack(MainActivity.class)) {
                ActivityUtils.startActivity(MainActivity.class, R.anim.right_in, R.anim.left_out);
                ActivityUtils.finishToActivity(MainActivity.class, false);
            } else {
                ActivityUtils.startActivity(MainActivity.class, R.anim.right_in, R.anim.left_out);
            }
        }
    }

    @Subscribe
    public void onStringEvent(MyEventBus event) {
        //处理完之后刷新数据
        String message = event.getMessgae();
        if (message.equals(MyConstant.REFRESH_FIRE_NOTICE_DATA)) {
            resetData();
        }

    }

    private OnTabSelectListener onTabSelectListener = new OnTabSelectListener() {
        @Override
        public void onTabSelect(int position) {
            viewPager.setCurrentItem(position, false);
        }

        @Override
        public void onTabReselect(int position) {

        }
    };

    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            commonTab.setCurrentTab(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    @OnClick({R.id.iv_filte, R.id.layout_right_drawer})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_right_drawer:
                //不做任何操作，防止点击穿透
                break;
            case R.id.iv_filte:
                boolean isOpen = drawerLayout.isDrawerOpen(layoutRightDrawer);
                if (isOpen) {
                    closeDrawers();
                } else {
                    openDrawer();
                }
                break;

        }
    }

    /**
     * 关闭侧边栏
     */
    public void closeDrawers() {
        drawerLayout.closeDrawer(layoutRightDrawer, true);
    }

    /**
     * 打开侧边栏
     */
    public void openDrawer() {
        drawerLayout.openDrawer(layoutRightDrawer);
    }

    /**
     * 重置数据
     */
    private void resetData() {
        SPStaticUtils.put(MySPTools.fireNoticeFliteBeginTime, "");
        SPStaticUtils.put(MySPTools.fireNoticeFliteEndTime, "");

        timeFilterTime.resetData();

        try {
            fireNoticeFragment1.resetData();
            fireNoticeFragment2.resetData();
            fireNoticeFragment3.resetData();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}